package lee.study.StackAndQueue;

import java.util.Arrays;

class MyStack<T> {
    public T[] elem;//数组
    public int top;//表示当前可以存储的数据元素的下标

    public MyStack() {
        this.elem = (T[]) new Object[10];
    }

    public void push(T data) {//入栈
        if (full()) {//判满
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.top] = data;
        this.top++;
    }

    public boolean empty() {//判空
        return this.top == 0;
    }

    private boolean full() {//判满
        return this.top == this.elem.length;
    }

    public T pop() {
        if (empty()) {
            throw new RuntimeException("栈空！");
        }
        T data = this.elem[this.top - 1];
        this.top--;
        return data;
        //return this.elem[--this.top];
    }

    public T peek() {
        if (empty()) {
            throw new RuntimeException("栈空！");
        }
        return this.elem[this.top - 1];
    }

    public int size() {
        return this.top;
    }
}
